package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size()>0){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
            setmealDishMapper.insertBatch(setmealDishes);

        }


    }

    @Override
    public PageResult get(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        //
        Page<SetmealVO> page = setmealMapper.select(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void delete(List<Long> ids) {
        ids.forEach(id->{
         Setmeal setmeal = setmealMapper.selectById(id);
         if (setmeal.getStatus()== StatusConstant.ENABLE){
                //起售的不能删除
             throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        for (Long id : ids) {
            //删除套餐数据
            setmealMapper.deleteById(id);

            //删除套餐菜品的数据
            setmealDishMapper.deleteBySetmealId(id);
        }

    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //TODO  起售 停售  一会补充
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }

    @Transactional
    @Override
    public void put(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐
        setmealMapper.update(setmeal);

        Long id = setmealDTO.getId();
        setmealDishMapper.deleteBySetmealId(id);

        //根据修改的setmealId 添加新的套餐和菜品的关系数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal  = setmealMapper.selectWithCategoryById(id);
       List<SetmealDish> setmealDishList =  setmealDishMapper.getSetmealDishById(id);
       SetmealVO setmealVO= new SetmealVO();
       BeanUtils.copyProperties(setmeal,setmealVO);
       setmealVO.setSetmealDishes(setmealDishList);
       return setmealVO;
    }

    @Override
    public List<Setmeal> user_select(Long categoryId) {
        List<Setmeal> setmealList = setmealMapper.selectByCategoryId(categoryId);
        return setmealList;
    }

    @Override
    public List<DishItemVO> user_getByid(Long id) {


       List<DishItemVO> dishItemVOList =  setmealDishMapper.selectById(id);
       return dishItemVOList;
    }
}
